from google.appengine.api import images
from google.appengine.api import urlfetch
from google.appengine.ext import blobstore
from google.appengine.ext.webapp import blobstore_handlers
import sys, mimetypes

from app.api import abstract

class Action(abstract.Action):
    
    def post(self):
        
        print self.request
        
        sys.exit()
    
    def get(self):
        
        url = "http://www.benm.at/wp-content/uploads/2008/07/iphone_3g_cracks_2.jpg"
        result = urlfetch.fetch(url)
        
        if result.status_code != 200:
            print "error"
            sys.exit()
            
        image = result.content
        
        avatar = images.resize(image_data=image, width=150, output_encoding=images.JPEG)
        avatar = images.im_feeling_lucky(image_data = avatar, output_encoding=images.JPEG)

        self.response.headers['Content-Type'] = "image/jpg"
        self.response.out.write(avatar)
        
        return

        sys.exit()

        upload_url = blobstore.create_upload_url('/tools/upload')
                        
        content_type, body = self.encode_multipart_formdata([], 'test.jpg', image)
        
        #print body
        
        #sys.exit()
        
        res = urlfetch.fetch(url = upload_url, payload='file=FieldStorage%28%27file%27%2C+%270_1425_sz_1_i_188238_14.jpg%27%29&submit=Submit', method = 'POST', 
                             headers={'Content-Type' : 'multipart/form-data; boundary=---------------------------2680948304491164042041866470'})
        
        print res.status_code
        print res.content

    def encode_multipart_formdata(self, fields=None, filename="", filedata=None):
        boundary = '-------tHISiStheMulTIFoRMbOUNDaRY'
        crlf = '\r\n'
        l = []
        if fields:
            for (key, value) in fields:
                l.append('--' + boundary)
                l.append('Content-Disposition: form-data; name="%s"' % str(key))
                l.append('')
                l.append(str(value))
                
        l.append('--' + boundary)
        l.append('Content-Disposition: form-data; name="media"; \
                      filename="%s"' % (str(filename),))
        l.append('Content-Type: %s' % self.get_content_type(filedata))
        l.append('')
        l.append(filedata)
            
        l.append('--' + boundary + '--')
        l.append('')
        body = crlf.join(l)
        content_type = 'multipart/form-data; boundary=%s' % boundary
        return content_type, body
        

    def get_content_type(self, file):
        return mimetypes.guess_type(file)[0] or 'application/octet-stream'
        
        
    def _get(self):
            upload_url = blobstore.create_upload_url('/tools/upload')
            
            self.response.out.write('<html><body>')
            self.response.out.write('<form action="%s" method="POST" enctype="multipart/form-data">' % upload_url)
            self.response.out.write("""Upload File: <input type="file" name="file"><br> <input type="submit" 
                name="submit" value="Submit"> </form></body></html>""")
